package com.luo.chengrui.module.sa.controller.admin.datapermission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.luo.chengrui.module.sa.controller.admin.datapermission.vo.*;
import com.luo.chengrui.module.sa.dal.dataobject.datapermission.DataPermissionDetailDO;
import com.luo.chengrui.module.sa.convert.datapermission.DataPermissionDetailConvert;
import com.luo.chengrui.module.sa.service.datapermission.DataPermissionDetailService;

@Tag(name = "管理后台 - 数据权限明细")
@RestController
@RequestMapping("/sa/data-permission-detail")
@Validated
public class DataPermissionDetailController {

    @Resource
    private DataPermissionDetailService dataPermissionDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建数据权限明细")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:create')")
    public CommonResult<String> createDataPermissionDetail(@Valid @RequestBody DataPermissionDetailCreateReqVO createReqVO) {
        return success(dataPermissionDetailService.createDataPermissionDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据权限明细")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:update')")
    public CommonResult<Boolean> updateDataPermissionDetail(@Valid @RequestBody DataPermissionDetailUpdateReqVO updateReqVO) {
        dataPermissionDetailService.updateDataPermissionDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据权限明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:delete')")
    public CommonResult<Boolean> deleteDataPermissionDetail(@RequestParam("id") String id) {
        dataPermissionDetailService.deleteDataPermissionDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据权限明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:query')")
    public CommonResult<DataPermissionDetailRespVO> getDataPermissionDetail(@RequestParam("id") String id) {
        DataPermissionDetailDO dataPermissionDetail = dataPermissionDetailService.getDataPermissionDetail(id);
        return success(DataPermissionDetailConvert.INSTANCE.convert(dataPermissionDetail));
    }

    @GetMapping("/list")
    @Operation(summary = "获得数据权限明细列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:query')")
    public CommonResult<List<DataPermissionDetailRespVO>> getDataPermissionDetailList(@RequestParam("ids") Collection<String> ids) {
        List<DataPermissionDetailDO> list = dataPermissionDetailService.getDataPermissionDetailList(ids);
        return success(DataPermissionDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据权限明细分页")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:query')")
    public CommonResult<PageResult<DataPermissionDetailRespVO>> getDataPermissionDetailPage(@Valid DataPermissionDetailPageReqVO pageVO) {
        PageResult<DataPermissionDetailDO> pageResult = dataPermissionDetailService.getDataPermissionDetailPage(pageVO);
        return success(DataPermissionDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据权限明细 Excel")
    @PreAuthorize("@ss.hasPermission('sa:data-permission-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportDataPermissionDetailExcel(@Valid DataPermissionDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DataPermissionDetailDO> list = dataPermissionDetailService.getDataPermissionDetailList(exportReqVO);
        // 导出 Excel
        List<DataPermissionDetailExcelVO> datas = DataPermissionDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据权限明细.xls", "数据", DataPermissionDetailExcelVO.class, datas);
    }

}
